<?php 
	
	include('config.php');

	// are we updating via Ajax? 
	if ( isset($_REQUEST['action']) ) {
		
		// define xmlrpc
		require_once ('xmlrpc-2.2.1/xmlrpc.inc');
		$xmlrpc = new xmlrpc_client ($url);
		$params [] = new xmlrpcval (0);
		$params [] = new xmlrpcval ($user);
		$params [] = new xmlrpcval ($pass);
		// set encoding for results
		header('Content-Type: text/html; charset=ISO-8859-1'); 
		
		/******* DELETE COMMENT **********
			Deletes comment via XMLRPC call
			Receives id[int] via GET
			Returns 'Message Deleted' or error message [string]
		****************************/
		if ( $_GET['action'] == 'delete' ) {
			// delete comment
			$params [] = new xmlrpcval ($_GET['id']);
			$delete_response = $xmlrpc->send(new xmlrpcmsg("wp.deleteComment", $params));
			if ( $delete_response->faultCode() ){
				die( $delete_response->faultString() );
			}else{
				die ( 'Message Deleted' );
			}
			
		/******* NEW COMMENT **********
			Posts new comment via XMLRPC
			Receives comment_parent[int], comment_content[int] via GET
			Returns 'Reply Posted' or error message [string]
			Note: Returns error if Parent Comment is not live
		****************************/					
		}else if ( $_POST['action'] == 'new_comment' ) {
			// new comment
			$params [] = new xmlrpcval ($_POST['comment_post_ID']);
			$params [] = new xmlrpcval( array( 
				'status' => new xmlrpcval('approve', "string"), 
				'comment_parent' => new xmlrpcval($_POST['comment_parent'], "int"), 
				'content' => new xmlrpcval($_POST['comment_content'], "string")
				 ), "struct");
			$new_comment_response = $xmlrpc->send(new xmlrpcmsg("wp.newComment", $params));
			if ( $new_comment_response->faultCode() ){
				die( 'error: '.$new_comment_response->faultString() );
			}else{
				die ( 'Reply Posted' );
			}
			
		/******* APPROVE OR SPAM **********
			Sets comment as SPAM or APPROVED via XMLRPC
			Receives action[string], id[int] via GET
			Returns 'Message Approved', 'Message Marked as Spam' or error message [string]
		****************************/
		}elseif(  $_GET['action'] == 'approve' || $_GET['action'] == 'spam' ){
			// update status
			$params [] = new xmlrpcval ($_GET['id']);
			$params [] = new xmlrpcval( array( "status" => new xmlrpcval($_GET['action'], "string") ), "struct");
			$edit_response = $xmlrpc->send(new xmlrpcmsg("wp.editComment", $params));
			if ( $edit_response->faultCode() ){
				die( $edit_response->faultString() );
			}else{
				if ( $_GET['action'] == 'approve' ) 
					die('Message Approved') ;
				else
					die('Message Marked as Spam');
			}
			
		/******* COMMENT FORM **********
			Gets first and last name of the user via XMLRPC then outputs comment reply form
			Comments are posted with the account info set in config.php
			Receives action[string], id[int] via GET
			Returns 'html form [string]
		****************************/
		}elseif( $_GET['action'] == 'comment_form' ) {
			// user info
			$user_info_response = $xmlrpc->send(new xmlrpcmsg("blogger.getUserInfo", $params));
			if ( $user_info_response->faultCode() ){
				die( $user_info_response->faultString() );
			}else{
				$info = php_xmlrpc_decode($user_info_response->value()); 
			}			
			die( '<form action="comments.php" method="post" class="commentform">
				<p>Posting as: '.$info['firstname'].' '.$info['lastname'].'</p>
				<p><textarea name="comment_content" id="comment_content" cols="45" rows="10" tabindex="4"></textarea></p
				<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
				<input type="hidden" name="comment_post_ID" id="comment_post_ID" value="'.$_GET['comment_post_id'].'" />
				<input type="hidden" name="comment_parent" id="comment_parent" value="'.$_GET['comment_parent'].'" />
				<input type="hidden" name="action" id="action" value="new_comment" />
				</p>
			</form>');
			
		/******* GET COUNT **********
			Gets total count of pending comments via XMLRPC
			Receives action[string] via GET
			Returns 'number count [int] or error
		****************************/
		}elseif( $_GET['action'] == 'get_count' ){
			// comments count
			$count_response = $xmlrpc->send(new xmlrpcmsg("wp.getCommentCount", $params));
			if ( $count_response->faultCode() ){
				die( $count_response->faultString() );
			}else{
				$count = php_xmlrpc_decode($count_response->value()); // returns array of 'approved, awaiting_moderation, total_comments
				die( $count['awaiting_moderation'] );
			}

		/******* GET COMMENMTS **********
			Gets pending comments via XMLRPC
			Receives  via GET
				number[int] - number of items to get
				offset[int] - offset number for paging
			Returns html list of comments [string] or error <li>error</li>
		****************************/
		}elseif( $_GET['action'] == 'get_comments' ){
			// comments
			$params [] = new xmlrpcval( array( 
				"status" => new xmlrpcval('hold', "string"), 
				"number" => new xmlrpcval($_GET['number'], "int"), 
				"offset" => new xmlrpcval($_GET['offset'], "int") ), "struct");
			$comments_response = $xmlrpc->send(new xmlrpcmsg("wp.getComments", $params));
			if ( $comments_response->faultCode() ){
				die($comments_response->faultString());
			}else{
				$comments = php_xmlrpc_decode($comments_response->value()); 
				foreach ( $comments as $comment ) {
					$author = ( $comment['author']!='') ? '<div class="author"><strong>'.$comment['author'].'</strong></div>' : '';
					$author_email = ( $comment['author_email']!='') ? '<div class="email">'.$comment['author_email'].'</div>' : '';
					$author_url = ( $comment['author_url']!='') ? '<div class="url"><a href="'.$comment['author_url'].'">'.$comment['author_url'].'</a></div>' : '';
					$author_ip = ( $comment['author_ip']!='') ? '<div class="author_ip">'.$comment['author_ip'].'</div>' : '';
					$date = $comment['date_created_gmt'];
					$year = substr($date, 0, 4);
					$month = substr($date, 4, 2);
					$day = substr($date, 6, 2);
					$hours = substr($date, 9, 2);
					$minutes = substr($date, 12, 2);
					$seconds = substr($date, 15, 2);					
					$time = date('M, j Y h:i:s A', mktime($hours, $minutes, $seconds, $month, $day, $year)); 
					
					list($protocol, $version) = split('/', $_SERVER['SERVER_PROTOCOL']);
					$baseurl = dirname(strtolower($protocol).'://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']);
					$grav_url = "http://www.gravatar.com/avatar.php?
					gravatar_id=".md5($comment['author_email']).
					"&default=".urlencode($baseurl.'/i/gravatar.jpg').
					"&size=40";
					$comments_list[] = '<li id="c-'.$comment['comment_id'].'">
					<div class="link-to-post"><a class="reply bg-img" href="./comments.php?action=comment_form&comment_parent='.$comment['comment_id'].'&comment_post_id='.$comment['post_id'].'" title="reply to this comment">Reply</a>Comment on: <a href="'.$comment['link'].'">'.$comment['post_title'].'</a> <br />
					<span class="time">'.$time.'</span></div>
					<div class="comment">
						'.$comment['content'].'
					</div>
					<div class="meta">
						<img src="'.$grav_url.'" alt="gravatar" /> 
						'.$author.$author_email.$author_url.$author_ip.'
					</div>
					<div class="actions"><a class="approve" href="./comments.php?action=approve&amp;id='.$comment['comment_id'].'">Approve</a>
						<a class="delete" href="./comments.php?action=delete&amp;id='.$comment['comment_id'].'">Delete</a> 
						<a class="spam" href="./comments.php?action=spam&amp;id='.$comment['comment_id'].'">Spam</a>
						
					</div>
					<br class="clear" />
					</li>';
				}
				echo ($comments_list) ? implode("\n", $comments_list) : '<li>No new messages</li>';
				die();
			}
		}
		
	}else{
		die('');
	}
?>
